<?php 
declare (strict_types = 1);
namespace app\admin\controller;
use app\common\controller\Admin;
use think\Request;
class OrderFleet extends Admin
{
    protected $table_id = false;
    protected $with = ['script','business','user_index','order_member'=>['user_index','business','virtual_characters']];
    public function initialize()
    {
        $this->menu_input = [
            'all' => [
            ],
            'add' => [
            ],
            'upload' => [
            ],
        ];
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    public function table_mage(&$data = [])
    {
        $data = [
            ['field' => 'create_time', 'title' => '组建时间'],
            ['field' => 'departure_time', 'title' => '发车时间'],
            ['field' => 'business_name', 'title' => '门店'],
            ['field' => 'script_name', 'title' => '剧本名称'],
            ['field' => 'status_name', 'title' => '状态'],
            ['title' => '操作', 'templet' => '#list_setting']
        ];
        return parent::table_mage($data); // TODO: Change the autogenerated stub
    }
    public function index()
    {
        $Business_model = app('app\admin\model\Business');
        $business = $Business_model->select()->toArray();
        $this->assign('business',$business);
        return parent::index(); // TODO: Change the autogenerated stub
    }

    //编辑-页面
    public function upload()
    {
        $id = input('id');
        $data = $this->model->where('id', $id)->with($this->with)->find()->toArray();
        if($data['status'] == 1){
            $name = '待发车';
        }elseif($data['status'] == 2){
            $name = '待到店';
        }elseif($data['status'] == 3){
            $name = '进行中';
        }elseif($data['status'] == 4){
            $name = '已完成';
        }elseif($data['status'] == 5){
            $name = '已解散';
        }else{
            $name = '';
        }
        $data['status_name'] = $name;
        $data['max']= $data['script_f_num'] + $data['script_m_num'];
        $data['min'] = count($data['order_member']);
        $this->assign('data', $data);
        return view();
    }
    //列表-数据接口
    public function datalist()
    {
        if (is_array($this->with)) {
            $with = $this->with;
        } else {
            $with = [];
        }
        $data = param();
        unset($data['page']);
        unset($data['limit']);
        unset($data['parent']);
        foreach ($data as $k => $v) {
            if (in_array($k, ['name', 'title'])) {
                $this->where[] = [$k, 'like', '%' . $v . '%'];
            } elseif ($k == 'create_time') {
                $arr = b_arr($v, '到');
                $first = trim($arr[0]);
                $last =  trim($arr[1]);
                $this->where[] = ['create_time', '>=', $first];
                $this->where[] = ['create_time', '<=', $last];
            } elseif ($k == 'departure_time') {
                $arr = b_arr($v, '到');
                $first = trim($arr[0]);
                $last =  trim($arr[1]);
                $this->where[] = ['departure_time', '>=', $first];
                $this->where[] = ['departure_time', '<=', $last];
            } elseif($k == 'user_name'){
                //根据用户昵称查询
                $UserIndex_model = app('app\admin\model\UserIndex');
                $user_index = $UserIndex_model->where('nickname','like',"%{$v}%")->select();
                if(!empty($user_index) && !empty($user_index->toArray()))
                {
                    $user_index = $user_index->toArray();
                    $id = [];
                    foreach ($user_index as $kk=>$vv)
                    {
                        $id[] = $vv['id'];
                    }
                    $id = implode(',',$id);
                    $this->where[] = ['user_index_id','in',$id];
                }else{
                    $this->where[] = ['user_index_id','=',-1];
                }
            } elseif($k == 'business_name'){
                //根据门店昵称查询
                $Business_model = app('app\admin\model\Business');
                $business_index = $Business_model->where('nickname','like',"%{$v}%")->select();
                if(!empty($business_index) && !empty($business_index->toArray()))
                {
                    $business_index = $business_index->toArray();
                    $id = [];
                    foreach ($business_index as $kk=>$vv)
                    {
                        $id[] = $vv['id'];
                    }
                    $id = implode(',',$id);
                    $this->where[] = ['business_id','in',$id];
                }else{
                    $this->where[] = ['business_id','=',-1];
                }
            } else {
                $this->where[] = [$k,'=',$v];
            }
        }
        $order = $this->order ?: 'id desc';
        $this->index_where($this->where);
        if(!empty($this->where)){
            $this->where = array_values($this->where);
        }
        $list = $this->model->where($this->where)->order($order)->with($with)->paginate($this->limit)->toArray();
        $this->index_handle($list);
        return layui($list['data'], $list['total']);
    }
    public function index_handle(&$data)
    {
        $res = $data['data'];
        foreach ($res as $k=>$v)
        {
            //状态
            if($v['status'] == 1){
                $status = '待发车';
            }elseif($v['status'] == 2){
                $status = '待到店';
            }elseif($v['status'] == 3){
                $status = '进行中';
            }elseif($v['status'] == 4){
                $status = '已完成';
            }elseif($v['status'] == 5){
                $status = '已解散';
            }else{
                $status = '';
            }
            $res[$k]['business_name'] = $v['business']['nickname'];
            $res[$k]['status_name'] = $status;
        }
        $data['data'] = $res;
        return parent::index_handle($data); // TODO: Change the autogenerated stub
    }
}